/*
 * MyQueue.c
 *
 *  Created on: 2012-2-21
 *      Author: joshua
 */

#include "MyQueue.h"

void initQueue(struct MyQueue *hq) {
	hq->front = hq->rear = NULL;
	hq->length = 0;
	return;
}

void enQueue(struct MyQueue *hq, elemType x) {
	struct sNode *newP;
	newP = malloc(sizeof(struct sNode));

	if (newP == NULL) {
		printf("malloc memory failure ");
		exit(1);
	}

	newP->data = x;
	newP->next = NULL;

	if (hq->rear == NULL) {
		hq->front = hq->rear = newP;
	} else {
		hq->rear->next = newP;
		hq->rear = newP;
	}
	hq->length++;
	return;
}

elemType outQueue(struct MyQueue *hq) {
	struct sNode *p;
	elemType temp;
	if (hq->front == NULL) {
		printf("Null Queue ");
		exit(1);
	}
	temp = hq->front->data;
	p = hq->front;
	hq->front = p->next;
	if (hq->front == NULL) {
		hq->rear = NULL;
	}
	free(p);
	hq->length--;
	return temp;
}

